/*******************************************************************************
 * BIP MAIN VIEW
 * 
 * Copyright:
 * 
 * License:
 * 
 * Authors: SALMON Charles
 * 
 ******************************************************************************/

/*******************************************************************************
 * 
 ******************************************************************************/

/**
 * The GUI definition of the bip application
 */
qx.Class.define("bip.MainView", {
	extend : qx.ui.container.Composite,

	/*
	 * ****************************************************************************
	 * CONSTRUCTOR
	 * ****************************************************************************
	 */

	construct : function() {
		this.base(arguments);

		// Configure layout
		var layout = new qx.ui.layout.VBox;
		layout.setSeparator("separator-vertical");
		this.setLayout(layout);

		// Header
		this.add(this.__createHeader());

		// Content
		var tabView = new qx.ui.tabview.TabView();

		var page1 = new qx.ui.tabview.Page("Users",
				"icon/16/apps/utilities-terminal.png");
		page1.setLayout(new qx.ui.layout.VBox());
		var userModel = bip.rpc.User.getInstance();
		page1.add(new bip.ui.users.UserProfile(userModel));
		tabView.add(page1);

		var page2 = new qx.ui.tabview.Page("Categories",
				"icon/16/apps/utilities-notes.png");
		page2.setLayout(new qx.ui.layout.VBox());
		
		// The service is responsible of fetching/posting categories data
		var service = new bip.services.CategoriesService();
		
		// SELECT CATEGORY
		var groupBox = new qx.ui.groupbox.GroupBox("Select a category");
		groupBox.setLayout(new qx.ui.layout.Grow());
		this.__categoriesList = new qx.ui.form.List();
		this.__categoriesList.setHeight(100);
		var categoriesController = new qx.data.controller.List(null, this.__categoriesList);
		categoriesController.setDelegate({
			createItem : function() {
				return new bip.ui.categories.CategoryListView();
			},
	    
			bindItem : function(controller, item, id) {
				item.setModel(controller.getModel().getItem(id));
				controller.bindProperty("name", "name", null, item, id);
				controller.bindProperty("description", "description", null, item, id);
			},
			
			filter : function( data) {
				if (data.getName() == null) { return false;}
				return true;
			}
		});
		service.bind("categories", categoriesController, "model");
		groupBox.add(this.__categoriesList);
		page2.add(groupBox);
		
		// EDIT CATEGORY
		groupBox = new qx.ui.groupbox.GroupBox("Edit category");
		groupBox.setLayout(new qx.ui.layout.Basic());
		this.__categoryForm = new bip.ui.categories.CategoryForm();
		groupBox.add(new qx.ui.form.renderer.Single(this.__categoryForm));
		page2.add(groupBox);
		var formController = new qx.data.controller.Form(null, this.__categoryForm,false);
		/*this.__categoriesList.addListener("changeSelection", function(evt) {
			var selectedItem = evt.getData()[0];
			// this.debug(qx.dev.Debug.debugProperties(selectedItem.getModel()));
			formController.setModel(selectedItem.getModel());
		}, this);*/
		categoriesController.bind("selection[0]",formController,"model");
		this.__categoryForm.getSaveButton().addListener("execute", function() {
			if (this.validate()) {
				alert("You are saving: " + qx.util.Serializer.toJson(formController.getModel()));
				//formController.updateModel();
			}
		}, this.__categoryForm);
		this.__categoryForm.getCancelButton().addListener("execute", this.__categoryForm.reset, this.__categoryForm);
		
		// PREVIEW CATEGORY
		groupBox = new qx.ui.groupbox.GroupBox("Preview category");
		groupBox.setLayout(new qx.ui.layout.Canvas());
		var categoryPreview = new bip.ui.categories.CategoryView();
		formController.bind("model.description",categoryPreview,"description");
		formController.bind("model.minvalue",categoryPreview,"minvalue");
		formController.bind("model.maxvalue",categoryPreview,"maxvalue");
		//formController.bind("model.minvalue",categoryPreview,"value");
		groupBox.add(categoryPreview);
		page2.add(groupBox);
		tabView.add(page2);

		tabView.setBarPosition("left");
		tabView.setSelection([ page2 ]);
		this.add(tabView, {
			flex : 1
		});
		
		service.fetchCategories();

	},

	/*
	 * ****************************************************************************
	 * PROPERTIES
	 * ****************************************************************************
	 */

	properties : {

	},

	/*
	 * ****************************************************************************
	 * MEMBERS
	 * ****************************************************************************
	 */

	members : {
		__categoriesList : null,
		__categoryForm : null,

		getCategoriesList : function() {
			return this.__categoriesList;
		},

		getCategoryForm : function() {
			return this.__categoryForm;
		},

		// ------------------------------------------------------------------------
		// CONSTRUCTOR HELPERS
		// ------------------------------------------------------------------------
		/**
		 * Creates the application header.
		 */
		__createHeader : function() {
			var layout = new qx.ui.layout.HBox();
			var header = new qx.ui.container.Composite(layout);
			header.setAppearance("app-header");

			var title = new qx.ui.basic.Label("BIP");
			var user = bip.rpc.RpcManager.getInstance().getUserName();
			var userNameLabel = new qx.ui.basic.Label("user:" + user);

			header.add(title);
			header.add(new qx.ui.core.Spacer, {
				flex : 1
			});
			header.add(userNameLabel);

			return header;
		}

	},

	/*
	 * ****************************************************************************
	 * DESTRUCTOR
	 * ****************************************************************************
	 */

	destruct : function() {
		this._disposeObjects();
		this.__categoriesList.dispose();
		this.__categoriesList = null;
		this.__categoryForm.dispose();
		this.__categoryForm = null;
	}
});
